//
//  CLTool.m
//  SJ
//
//  Created by 程龙 on 2023/3/13.
//

#import "CLTool.h"
#import "SAMKeychain.h"
@implementation CLTool

//设备唯一标示
+(NSString *)cl_defaultUIDCreate
{
    NSString *uuidStrKey = [SAMKeychain passwordForService:@"ID" account:@"device"];
    if (uuidStrKey.length)
    {
        return uuidStrKey;
    }
    CFUUIDRef replaceuuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, replaceuuid );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    [SAMKeychain setPassword:result forService:@"ID" account:@"device"];
    CFRelease(replaceuuid);
    CFRelease(uuidString);
    return result;
}
//保存用户信息
+(void)cl_saveUser:(NSString *)userStr key:(NSString *)key{

  //  [SAMKeychain setPassword:userStr forService:key account:@"user"];
    
    
                
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:userStr forKey:key];
          

}
//获取用户信息
+(NSString *)cl_getUser:(NSString *)key{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *strKey = [ user objectForKey:key];
         
    
//    NSString *strKey = [SAMKeychain passwordForService:key account:@"user"];
    if (strKey.length)
    {
        return strKey;
    }
    return @"";
}
+ (void)cl_showLoading{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    Dialog()
    //加载框颜色
    .wLoadingColorSet(DialogColor(0xFF9900))
    //文字
    .wTitleSet(@"加载中...")
    .wTypeSet(DialogTypeLoading)
    //加载框type
    .wLoadingTypeSet(LoadingStyleWait)
    //加载框线条宽度
    .wLoadingWidthSet(2.5*CLSCALE)
   // .wShadowAlphaSet(0.02)
    //动画时间
    .wAnimationDurtionSet(1)
    //加载框大小
    .wWidthSet(100*CLSCALE)
    .wHeightSet(100*CLSCALE)
    .wMainOffsetYSet(10*CLSCALE)
    .wLoadingSizeSet(CGSizeMake(30*CLSCALE, 30*CLSCALE))
    .wTitleFontSet(12.0*CLSCALE)
    .wShadowCanTapSet(NO)
    .wStart();
}
+ (void)cl_showSuccess:(NSString *)content block:(nullable inishBlock)block{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if ([content isEqualToString:@""]) {
        content = @"成功";
    }
    [WMZDialog closeWithshowView:nil block:^{

    }];
    Dialog()
    //加载框颜色
    .wLoadingColorSet(DialogColor(0x108ee9))
    .wTitleSet(content)
    //加载框线条宽度
    .wLoadingWidthSet(2.5*CLSCALE)
    .wTypeSet(DialogTypeLoading)
    //加载框type
    .wLoadingTypeSet(LoadingStyleRight)
    //动画时间
    .wAnimationDurtionSet(0.8)
    //加载框大小
    .wWidthSet(100*CLSCALE)
    .wHeightSet(100*CLSCALE)
    .wMainOffsetYSet(10*CLSCALE)
    .wLoadingSizeSet(CGSizeMake(30*CLSCALE, 30*CLSCALE))
    .wTitleFontSet(12.0*CLSCALE)
    .wShadowCanTapSet(NO)
    .wStart();
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [WMZDialog closeWithshowView:nil block:^{
            
        }];
        block();
    });
}
+ (void)cl_showFail:(NSString *)content block:(nullable inishBlock)block{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if ([content isEqualToString:@""]) {
        content = @"失败";
    }
    [WMZDialog closeWithshowView:nil block:^{

    }];
    Dialog()
    //加载框颜色
    .wLoadingColorSet([UIColor redColor])
    .wTitleSet(content)
    .wLoadingWidthSet(2.5*CLSCALE)
    .wTypeSet(DialogTypeLoading)
    //加载框type
    .wLoadingTypeSet(LoadingStyleError)
    //动画时间
    .wAnimationDurtionSet(1)
    //加载框大小
    .wWidthSet(100*CLSCALE)
    .wHeightSet(100*CLSCALE)
    .wMainOffsetYSet(10*CLSCALE)
    .wLoadingSizeSet(CGSizeMake(30*CLSCALE, 30*CLSCALE))
    .wTitleFontSet(12.0*CLSCALE)
    .wShadowCanTapSet(NO)
    .wStart();
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [WMZDialog closeWithshowView:nil block:^{

        }];
        block();
    });
}
+ (void)cl_closeWithshowView{
    [WMZDialog closeWithshowView:nil block:^{

    }];
}

+ (NSString *)getRandomStringWithNum:(NSInteger)num
{
    NSString *string = [[NSString alloc]init];
    for (int i = 0; i < num; i++) {
        int number = arc4random() % 36;
        if (number < 10) {
            int figure = arc4random() % 10;
            NSString *tempString = [NSString stringWithFormat:@"%d", figure];
            string = [string stringByAppendingString:tempString];
        }else {
            int figure = (arc4random() % 26) + 97;
            char character = figure;
            NSString *tempString = [NSString stringWithFormat:@"%c", character];
            string = [string stringByAppendingString:tempString];
        }
    }
    return string;
}
/*
 * 获取当前VC
 */
+ (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    return currentVC;
}

+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    if ([rootVC presentedViewController]) {
        rootVC = [rootVC presentedViewController];
    }
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
    } else {
        currentVC = rootVC;
    }
    return currentVC;
}
+ (BOOL)isNullToDictionary:(NSDictionary *)dict
{

    if (dict != nil && ![dict isKindOfClass:[NSNull class]] && dict.count != 0){

        return NO;

    }else{

        return YES;
    }
}

@end
